part of 'page.dart';

class _CustomLexicalItemCreationPageView extends StatelessWidget {
  const _CustomLexicalItemCreationPageView({
    required _CustomLexicalItemCreationPageWloc wloc,
  }) : _wloc = wloc;

  final _CustomLexicalItemCreationPageWloc _wloc;

  Widget _buildContent({
    required BuildContext context,
  }) {
    return Column(
      children: [
        Expanded(
          child: Scrollbar(
            child: PageTileHorizontalPadding(
              child: SingleChildScrollView(
                child: SafeArea(
                  left: false,
                  top: false,
                  right: false,
                  bottom: true,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildLexicalItemTileGroup(
                        context: context,
                      ),
                      ..._buildUseCases(
                        context: context,
                      ),
                      const TileGap(),
                      _buildUseCaseAdditionTileGroup(
                        context: context,
                      ),
                      const TileGap(),
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
        PlatformKeyboardPlaceholder(
          backgroundColor: CoreTheme.of(context).backgroundColor,
        ),
      ],
    );
  }

  Widget _buildLexicalItemTileGroup({
    required BuildContext context,
  }) {
    final presentationModel = _wloc.customLexicalItemInputPresentationModel;

    return Column(
      key: presentationModel.globalKey,
      children: [
        const TileGap(),
        RoundedTileGroup(
          tiles: [
            ShakeBuiltTransition(
              controller: presentationModel.titleShakeAnimationController,
              child: TileTextField(
                autocorrect: true,
                enableSuggestions: true,
                controller: presentationModel.titleTextEditingController,
                focusNode: presentationModel.titleTextFieldFocusNode,
                keyboardType: TextInputType.text,
                placeholder: HomeCoreTranslation.of(context).lexicalItemTitlePlaceholder,
                textInputAction: TextInputAction.next,
                onSubmitted: _wloc.onSubmitLexicalItemTitle,
              ),
            ),
            ShakeBuiltTransition(
              controller: presentationModel.translationShakeAnimationController,
              child: TileTextField(
                autocorrect: true,
                enableSuggestions: true,
                controller: presentationModel.translationTextEditingController,
                focusNode: presentationModel.translationTextFieldFocusNode,
                keyboardType: TextInputType.text,
                placeholder: HomeCoreTranslation.of(context).lexicalItemTranslationPlaceholder,
                textInputAction: TextInputAction.next,
                onSubmitted: _wloc.onSubmitLexicalItemTranslation,
              ),
            ),
            TileTextField(
              autocorrect: false,
              enableSuggestions: false,
              controller: presentationModel.transcriptionTextEditingController,
              focusNode: presentationModel.transcriptionTextFieldFocusNode,
              keyboardType: TextInputType.text,
              placeholder: HomeCoreTranslation.of(context).lexicalItemTranscriptionPlaceholder,
              textInputAction: TextInputAction.next,
              onSubmitted: _wloc.onSubmitLexicalItemTranscription,
            ),
          ],
        ),
      ],
    );
  }

  List<Widget> _buildUseCases({
    required BuildContext context,
  }) {
    final useCaseWidgets = <Widget>[];
    final useCaseInputPresentationModels =
        _wloc.customLexicalItemInputPresentationModel.useCaseInputPresentationModels;

    for (var i = 0; i < useCaseInputPresentationModels.length; i++) {
      final useCaseWidget = (int index) {
        final presentationModel = useCaseInputPresentationModels[index];

        return _UseCaseAnimatedView(
          key: presentationModel.globalKey,
          presentationModel: presentationModel,
          onExampleSubmitted: () {
            _wloc.onSubmitUseCaseExample(
              index: index,
            );
          },
          onTranslationSubmitted: () {
            _wloc.onSubmitUseCaseTranslation(
              index: index,
            );
          },
          onUseCaseRemove: () {
            _wloc.onUseCaseRemovalButtonPressed(
              index: index,
            );
          },
        );
      }(i);

      useCaseWidgets.add(useCaseWidget);
    }
    return useCaseWidgets;
  }

  Widget _buildUseCaseAdditionTileGroup({
    required BuildContext context,
  }) {
    return RoundedTileGroup(
      tiles: [
        ActionTile(
          title: HomeCoreTranslation.of(context).useCaseAdditionTileTitle,
          icon: Icon(
            CustomRegularIcons.plus,
            size: 25,
            color: CoreTheme.of(context).primaryColor,
          ),
          onPressed: _wloc.onUseCaseAdditionButtonPressed,
        )
      ],
      footer: TileGroupFooter(
        title: HomeCoreTranslation.of(context).useCaseAdditionTileGroupFooter,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: CoreTheme.of(context).backgroundColor,
      navigationBar: CupertinoNavigationBar(
        backgroundColor: CoreTheme.of(context).navigationBarColor,
        brightness: CoreTheme.of(context).brightness,
        automaticallyImplyLeading: false,
        automaticallyImplyMiddle: false,
        padding: EdgeInsetsDirectional.zero,
        middle: NavigationBarTitle(
          title: CustomLexicalItemCreationPageTranslation.of(context).title,
        ),
        leading: ScaledNavigationBarTextButton(
          text: CoreTranslation.of(context).navigationBarCancelButtonTitle,
          navigationButtonAutoSizeGroup: _wloc.navigationBarTextButtonAutoSizeGroup,
          onPressed: Navigator.of(context).pop,
        ),
        trailing: ScaledNavigationBarTextButton(
          text: CoreTranslation.of(context).navigationBarCreateButtonTitle,
          isDefault: true,
          navigationButtonAutoSizeGroup: _wloc.navigationBarTextButtonAutoSizeGroup,
          onPressed: _wloc.onSubmit,
        ),
      ),
      child: SizedBox.expand(
        child: _buildContent(
          context: context,
        ),
      ),
    );
  }
}
